'use strict';

const Controller = require('egg').Controller;

class BookController extends Controller {
  async page() {
  //  const stores = await this.ctx.service.wms.basic.store.index();
    //  const router = await this.ctx.service.wms.basic.store.index();

    await this.ctx.render('/wms/basic/book.tpl');
  }
  async index() {
    // const { msg, msgType } = this.ctx.query;
    // 显示所有记录
    // 支持从 /system/account/index?offset=0&limit=20 来分页
    const { ctx } = this;
    const query = ctx.query;

    query.page = query.page === 'NaN' ? 0 : query.page;
    query.offset = query.offset ? parseInt(query.offset) : 0;
    query.limit = query.limit ? parseInt(query.limit) : 999;
    query.sortOrder = query.sortOrder || 'desc';

    const data = await ctx.service.wms.basic.book.index(query);

    ctx.set('Content-Type', 'application/json');
    if (typeof (data) === 'string') {
      ctx.state = 500;
      ctx.body = data;
    } else {
      data.total = data.count;// 兼容bootstrap-table 记录总条数字段
      ctx.state = 200;
      ctx.body = data;
    }
  }

  async show() {
    // /posts/:id	post
    const { ctx } = this;
    const id = ctx.params.id;
    ctx.set('Content-Type', 'application/json');
    if (!id) {
      ctx.state = 403;
      ctx.body = { code: 403, msg: '参数不正确！' };
    } else {
      const data = await ctx.service.wms.basic.book.show(id);
      ctx.state = 200;
      ctx.body = { code: 200, data };
    }
  }

  async create() {
    // /posts	posts
    // post 方式新增
    const { ctx } = this;
    const data = ctx.request.body;
    ctx.set('Content-Type', 'application/json');
    try {

      await ctx.service.wms.basic.book.new(data);
      ctx.state = 201;
      ctx.body = { code: 201, msg: '新增成功！' };
    } catch (e) {
      ctx.state = 500;
      ctx.body = { code: 500, msg: '新增失败！' + e.message };
    }
  }

  async update() {
    // /posts/:id	post
    // put 方式更新
    const { ctx } = this;
    const { name, value, pk } = ctx.request.body;
    const id = pk || ctx.params.id;
    const data = pk ? { [name]: value } : ctx.request.body;

    ctx.set('Content-Type', 'application/json');
    if (!id || !data) {
      ctx.state = 403;
      ctx.body = { code: 403, msg: '参数不正确！' };
    } else {
      try {
        await ctx.service.wms.basic.book.edit(id, data);
        ctx.state = 200;
        ctx.body = { code: 200, msg: '更新成功！' };
      } catch (e) {
        ctx.state = 500;
        ctx.body = { code: 500, msg: '更新失败！' + e.message };
      }
    }
  }

  async destroy() {
    // /posts/:id	post
    // delete 方式删除
    const { ctx } = this;
    const id = ctx.params.id;
    ctx.set('Content-Type', 'application/json');

    if (!id) {
      ctx.state = 403;
      ctx.body = { code: 403, msg: '参数不正确！' };
    } else {
      try {
        await ctx.service.wms.basic.book.destroy(id);
        ctx.state = 200;
        ctx.body = { code: 200, msg: '记录删除成功！' };
      } catch (e) {
        ctx.state = 500;
        ctx.body = { code: 500, msg: '记录删除失败！' + e.message };
      }
    }
  }
}

module.exports = BookController;
